const {
    toUrl,
    msgLoading,
    globalData,
    toShowToast
} = getApp()

// var myPluginInterface = requirePlugin('myPlugin');

import {
    request
} from '@/utils/request'
Page({
    toUrl,
    toShowToast,
    data: {
        circle: '',
        handleTimes: [1, 10, 10],
        seckill: [1, 2],
        globalData,
        show: false,
        banner: [],
        dialogBanner: [],
        activeList: [],
        goods: [], //推荐商品
        integralgoods: [], //积分商品
        suppliers: [], //推荐门店
        movieappid: '', //一键观影APPID
        marketing: {}, //营销页面信息
        current: 0,
        activeListFlag: false,
        headBc: '',
        kkkk: false,
        timer: null,
        jifenstatus: 0,
        keywords: ''
    },
    miaosha(data) {
        let {
            url,
            num,
            buy_num,
            quota_num,
            status
        } = data.currentTarget.dataset
        // console.log(data,num);
        // if (status == 2) {
        //     wx.showToast({
        //         title: '已结束',
        //         icon: 'none'
        //     })
        //     return
        // }
        // if (num == 0) {
        //     wx.showToast({
        //         title: '库存不足',
        //         icon: 'none'
        //     })
        //     return
        // }
        // if (buy_num >= quota_num && quota_num != 0) {
        //     wx.showToast({
        //         title: `限购数量${buy_num}`,
        //         icon: 'none'
        //     })
        //     return
        // }
        // if (status == 0) {
        //     wx.showToast({
        //         title: `秒杀暂未开始`,
        //         icon: 'none'
        //     })
        //     return
        // }
        wx.navigateTo({
            url: url,
        })
    },
    //处理头部滚动事件
    onPageScroll(e) {
        const scrollTop = e.scrollTop
        if (scrollTop > 100) {
            this.setData({
                headBc: 'headbc',
                act: '/image/icon-arrow-left.png'
            })
        } else {
            this.setData({
                headBc: '',
                act: '/img/act3.png'
            })
        }
    },
    changeSwiper({
        detail
    }) {
        const {
            current
        } = detail;
        this.setData({
            current
        });
    },
    getTimes() {
        return 111
    },
    getIndex() {
        console.log('开始了#################');
        const that = this
        request('index/index').then(res => {
            const {
                goods,
                integralgoods,
                marketing,
                movieappid,
                suppliers,
                seckill,
                circle
            } = res.data
            that.setData({
                goods: goods,
                integralgoods: integralgoods,
                marketing: marketing,
                movieappid: movieappid,
                suppliers: suppliers,
                seckill: seckill,
                circle: circle
            })

            console.log(circle, '##########');
            // that.autghons()


            if (seckill.length > 0) {
                this.timer = setInterval(() => {
                    this.handleSeckill() //秒杀处理时间
                }, 1000);
            }
        })
    },
    handleSeckill() {
        let dataList = this.data.seckill
        let times = null
        dataList.forEach((it) => {
            if (it.status == 0) {
                times = it.start_time

            } else {
                times = it.end_time

            }
            if (it.status == 0) {
                // console.log(it.dataList);
                if (it.dataList == undefined) {
                    // it.status = 21
                    1
                } else {
                    if (it.dataList[0] == '0' && it.dataList[1] == '00' && it.dataList[2] == '00') {
                        it.dataList = []
                        // console.log('我进来了');
                        times = it.end_time
                        it.status = 1
                    } else {
                        // console.log('失败了');
                        times = it.start_time
                    }
                }
            }
            if (it.status == 1) {
                times = it.end_time
            }




            let handleTimes = this.timestampToTime(times)
            it.dataList = handleTimes
        })
        this.setData({
            seckill: dataList,
        })
    },

    // 时间戳：1637244864707
    /* 时间戳转换为时间 */
    timestampToTime(endTime) {
        const stime = new Date().getTime();
        const etime = new Date(endTime) * 1000;
        const diffTime = etime - stime;
        const day = Math.floor(diffTime / (1000 * 60 * 60 * 24));
        const hour = Math.floor(diffTime / (1000 * 60 * 60) % 24) + day * 24;
        const minute = Math.floor(diffTime / (1000 * 60) % 60);
        const second = Math.floor(diffTime / 1000 % 60);
        return [hour, minute, second]
    },
    iiiii() {
        console.log(1111);
    },
    getBannerlist(type) {
        const data = {
            typeid: type
        }
        return request('index/bannerlist', {
            data
        })
    },
    getActiveList() {
        const that = this
        const data = {
            sort: 0,
            page: 1
        }
        request('activity/activitylist', {
            data
        }).then(res => {
            let activeListFlag = null
            activeListFlag = res.data.forEach(it => {
                if (it.status != 2) {
                    that.setData({
                        activeListFlag: true
                    })
                }
            })
            that.setData({
                activeList: that.data.activeList.concat(res.data)
            })
        }).finally(() => {
            wx.hideLoading()
        })
    },
    show() {
        this.setData({
            show: !this.data.show
        })
    },
    //时间处理
    handleDate() {
        const that = this
        wx.getStorage({
            key: '_userInfo',
            success(res) {
                // console.log(res.data)
                const date = new Date()
                const year = date.getFullYear()
                const month = date.getMonth() + 1
                const day = date.getDate()
                const time = `${year}-${month}-${day} 23:59:59`
                //今天的毫秒数
                const today = new Date(time).getTime()
                wx.getStorage({
                    key: 'time',
                    success(res) {
                        const storageTime = res.data
                        //只判断设备储存的时间，判断用户须带上token
                        if (storageTime) {
                            //储存的时间和今天时间一样，说明弹窗banner展示过了
                            if (storageTime == today) {
                                // console.log('已展示');
                                return
                            } else {
                                wx.setStorage({
                                    key: 'time',
                                    data: today
                                })
                                this.setData({
                                    show: true
                                })
                            }
                        }
                        // else {
                        //     wx.setStorage({key: 'time',data:today})
                        //         this.setData({
                        //             show: true
                        //         })
                        // }
                    },
                    fail(err) {
                        console.log(err)
                        wx.setStorage({
                            key: 'time',
                            data: today
                        })
                        that.setData({
                            show: true
                        })
                    }
                })
            },
            fail: function (err) {
                console.log(err);
                return
            }
        })
    },
    //一键观影
    goOther() {
        wx.navigateToMiniProgram({
            appId: this.data.movieappid,
            path: 'pages/index/index?q=https://b.taopiaopiao.cn/5jsyerHFOyEyeMpXQANZO1/60786',
            success(res) {
                // 打开成功
                console.log('成功');
            }
        })
    },
    onLoad(options) {
        msgLoading()
        const that = this
        this.getBannerlist(2).then(res => {
            that.setData({
                banner: that.data.banner.concat(res.data)
            })
        })
        this.getBannerlist(1).then(res => { //未处理，弹窗一天只弹一次
            that.setData({
                dialogBanner: that.data.dialogBanner.concat(res.data)
            })
        })
        this.handleDate()

        this.getActiveList()
    },

    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */

    // autghons() {
    //     console.log(this.data.circle, '@@@@@@@@@@@@');
    //     let that = this
    //     let {
    //         mchid,
    //         is_login,
    //         openid
    //     } = that.data.circle
    //     let mch_id = mchid
    //     if (is_login == 1) {

    //         myPluginInterface.getLocation(openid).then(res => {
    //             console.log(res)
    //         })

    //         //登录中
    //         myPluginInterface.getAuthStatus(openid, mch_id).then(res => {
    //             console.log(res.status)
    //             that.setData({
    //                 jifenstatus: res.status
    //             })
    //         }).catch((err) => {
    //             console.log(err);
    //         })
    //     } else {
    //         console.log('未登录');
    //     }
    // },
    handleWugan() {
        if (this.data.circle.is_login == 0) {
            wx.getStorage({
                key: "_userInfo",
                success: ({
                    data
                }) => {
                    console.log(data);
                    setTimeout(() => {
                        wx.redirectTo({
                            url: `/pages/home/home`,
                        })
                    }, 0);

                },
                fail: (err) => {
                    wx.showModal({
                        title: "登录",
                        content: "您好,该功能登录后使用！",
                        confirmColor: "#286BFB",
                        cancelColor: "#666",
                        confirmText: "去登录",
                        success(res) {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '/pages/login/login',
                                })
                            }

                        }
                    })

                }
            })
        } else {
            wx.navigateTo({
                url: `plugin://myPlugin/index?openid=${this.data.circle.openid}&mch_id=${this.data.circle.mchid}&member_status=${this.data.jifenstatus}`,
            })
        }

    },
    onShow() {
        this.getIndex()
        this.handleDate()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearInterval(this.timer)
        this.setData({
            timer: null
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.timer)
        this.setData({
            timer: null
        })


    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    handleSou01() {
        wx.navigateTo({
            url: `/pages/store/list?keywords=${this.data.keywords}`,
        })
    },
    blur({
        detail
    }) {
        this.setData({
            keywords: detail.value
        })

    },
})